package com.roocon.design_pattern.chapter05_guard_suspension;

import java.util.Random;

public class ServiceThread extends Thread {

  private final RequestQueue queue;
  private final Random random;

  private volatile boolean flag = true;

  public ServiceThread(RequestQueue queue) {
    this.queue = queue;
    this.random = new Random(System.currentTimeMillis());
  }

  @Override public void run() {
    while (flag) {
      Request request = queue.getRequest();
      if (null == request) {
        System.out.println("Received the empty request !");
        continue;
      }

      System.out.println("Server -> " + request.getValue());

      try {
        Thread.sleep(random.nextInt(1_000));
      } catch (InterruptedException e) {
        e.printStackTrace();
        return;
      }
    }
  }

  public void close() {
    this.flag = false;
    this.interrupt();
  }

}
